$(document).ready(function(){

    var VAL ={
        NORMAL     : "0000000"  ,
        EVT_USER   : $("#userId").text(),
        T_FBPBISOPE	 : 'FBPBISOPE',
        T_FBPBISTOL  : 'FBPBISTOL',
        T_FTPTOLOPE  : 'FTPTOLOPE',
        T_MTPTOOLPM  : 'MTPTOOLPM',
        T_FBPBISDAT  : 'FBPBISDAT',
        T_HIPTOLHIS  : 'HIPTOLHIS',
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };
    var domObj = {
        $portListDiv:$("#portListDiv"),
        $portListGrd:$("#portListGrd"),
        portListPg	:"portListPg",

        $selNoListDiv:$("#selNoListDiv"),
        $selNoListGrd:$("#selNoListGrd"),
        selNoListPg	:"selNoListPg",

        $toolStatHisListDiv:$("#toolStatHisListDiv"),
        $toolStatHisListGrd:$("#toolStatHisListGrd"),
        toolStatHisListPg	:"toolStatHisListPg",

        $portStatListDiv:$("#portStatListDiv"),
        $portStatListGrd:$("#portStatListGrd"),
        portStatListPg	:"portStatListPg",

        $toolIdSelDiv:$("#toolIdSelDiv"),
        $toolSelListGrd:$("#toolSelListGrd"),
        toolSelListPg:"toolSelListPg",

        $reasonGridDiv:$("#reasonGridDiv"),
        $reasonListGrd:$("#reasonListGrd"),
        reasonListPg:"reasonListPg",

        $opeNoSel : $("#opeNoSel"),
        $toolSel  : $("#toolSel"),
        $remarkInfo:$("#remarkInfo"),
        $pmEvtSeqId:$("#pmEvtSeqId"),
        buttons:{
            $tool_trns_btn    :$("#tool_trns_btn"),
            $tool_pm_btn   :$("#tool_pm_btn"),
            $port_trns_btn    :$("#port_trns_btn"),
            $tool_lock_btn    :$("#tool_lock_btn"),
            $tool_unlock_btn  :$("#tool_unlock_btn"),
            $tool_hold_btn    :$("#tool_hold_btn"),
            $tool_unhold_btn  :$("#tool_unhold_btn"),
            $tool_remark_btn  :$("#tool_remark_btn"),
            $tool_trnMode_btn :$("#tool_trnMode_btn"),
            $query_tool_his_btn : $("#query_tool_his_btn")
        },
        spans:{
            $toolIdSpan        :      $("#toolIdSpan")        ,
            $toolDscSpan       :      $("#toolDscSpan")       ,
            $rotTolIdSpan      :      $("#rotTolIdSpan")      ,
            $parentTolSpan     :      $("#parentTolSpan")     ,
            $tolCateSpan       :      $("#tolCateSpan")       ,
            $tolModeSpan       :      $("#tolModeSpan")       ,
            $trnsCateSpan      :      $("#trnsCateSpan")      ,
            $tolStatSpan       :      $("#tolStatSpan")       ,
            $tolChdStatSpan    :      $("#tolChdStatSpan")    ,
            $statDscSpan       :      $("#statDscSpan")       ,
            $ctrlDscSpan       :      $("#ctrlDscSpan")       ,
            $recipeIdSpan      :      $("#recipeIdSpan")      ,
            $frtTolChdSpan     :      $("#frtTolChdSpan")     ,
            $rcUpdUsrSpan      :      $("#rcUpdUsrSpan")      ,
            $rcUpdTimSpan      :      $("#rcUpdTimSpan")      ,
            $loadEndTimeSpan   :      $("#loadEndTimeSpan")   ,
            $unloadEndTimeSpan :      $("#unloadEndTimeSpan"),
            $lockFlgSpan       :      $("#lockFlgSpan"),
            $holdFlgSpan	   :      $("#holdFlgSpan")
        }
    };
    var controlsFunc={
            iniToolStatHisInfo: function(){
                var itemInfoCM = [
                    {
                        name  : 'tool_id_fk',
                        index : 'tool_id_fk',
                        label : TOOLG_ID_TAG,
                        width : 100
                    },{
                        name : 'tool_stat',
                        index :'tool_stat',
                        label : TOOL_STATUS_TAG,
                        width : 100
                    },{
                        name  : 'tool_sub_stat',
                        index : 'tool_sub_stat',
                        label : '设备子状态',
                        width : 100
                    },{
                        name : 'tool_lock_flg',
                        index :'tool_lock_flg',
                        label : "设备锁定标识",
                        width : 100
                    },{
                        name  : 'tool_hold_flg',
                        index : 'tool_hold_flg',
                        label : "设备状态hold标识",
                        width : 100
                    },{
                        name : 'tool_run_mode',
                        index :'tool_run_mode',
                        label : TOOL_RUN_MODEL_TAG,
                        width : 100
                    },{
                        name  : 'ld_rtd_flg',
                        index : 'ld_rtd_flg',
                        label : "load端自动派工标识",
                        width : 80,
                        hidden:true
                    },{
                        name : 'ul_rtd_flg',
                        index :'ul_rtd_flg',
                        label : "unload端自动派工标识",
                        width : 80,
                        hidden:true
                    },{
                        name  : 'pv_tool_stat',
                        index : 'pv_tool_stat',
                        label : "上次设备状态",
                        width : 100
                    },{
                        name : 'pv_tool_sub_stat',
                        index :'pv_tool_sub_stat',
                        label : "上次子设备状态",
                        width : 100
                    },{
                        name  : 'clm_timestamp',
                        index : 'clm_timestamp',
                        label : XFR_CLM_TIME_TAG,
                        width : 80,
                        hidden: true
                    },{
                        name : 'keep_time',
                        index :'keep_time',
                        label : "持续时间",
                        width : 80,
                        hidden: true
                    },{
                        name  : 'ld_last_fetch_timestamp',
                        index : 'ld_last_fetch_timestamp',
                        label : "load端最后抽片时间",
                        width : 80,
                        hidden: true
                    },{
                        name : 'ul_last_fetch_timestamp',
                        index :'ul_last_fetch_timestamp',
                        label : "unload端最后抽片时间",
                        width : 80,
                        hidden: true
                    },{
                        name  : 'evt_usr',
                        index : 'evt_usr',
                        label : EVT_USR,
                        width : 100
                    }
                ];
                domObj.$toolStatHisListGrd.jqGrid({
                    datatype : "local",
                    autoheight : true,
                    mtype : "POST",
                    height : 270,
                    autowidth : true,//宽度根据父元素自适应
                    shrinkToFit : false,
                    scroll : true,
                    resizable : true,
                    rownumbers : true,
                    loadonce : true,
                    viewrecords : true,
                    colModel : itemInfoCM,
//				multiselect : true,
                    pager : domObj.toolStatHisListPg,
                    gridComplete : function() {
                    },
                    onSelectRow : function(id) {
                    }
                });
            },
        iniToolGridInfo:function(){
            var itemInfoCM = [
                {
                    name  : 'tool_id',
                    index : 'tool_id',
                    label : TOOL_ID_TAG,
                    width : 250
                },{
                    name : 'tool_dsc',
                    index :'tool_dsc',
                    label : TOOL_DSC_TAG,
                    width : 250
                }
            ];
            domObj.$toolSelListGrd.jqGrid({
                datatype : "local",
                autoheight : true,
                mtype : "POST",
                height : 270,
                // autowidth : true,//宽度根据父元素自适应
                width:$(".container-fluid").width()*0.4,
                shrinkToFit : false,
                scroll : true,
                resizable : true,
                rownumbers : true,
                loadonce : true,
                viewrecords : true,
                colModel : itemInfoCM,
//				multiselect : true,
                pager : domObj.toolSelListPg,
                gridComplete : function() {
                },
                onSelectRow : function(id) {
                }

            });
        },
        iniSelNoGridInfo:function(){
            var itemInfoCM = [
                {
                    name  : 'data_ext',
                    index : 'data_ext',
                    label : NO_TAG,
                    width : 80
                },{
                    name  : 'data_item',
                    index : 'data_item',
                    label : NOCH_TAG,
                    width : 80
                },{
                    name : 'data_desc',
                    index :'data_desc',
                    label : NO_DSC_TAG,
                    width : 80
                }
            ];
            domObj.$selNoListGrd.jqGrid({
                datatype : "local",
                autoheight : true,
                mtype : "POST",
                height : 270,
               /* autowidth : true,//宽度根据父元素自适应*/
                width:$(".container-fluid").width()*0.4,
                shrinkToFit : true,
                scroll : true,
                resizable : true,
                rownumbers : true,
                loadonce : true,
                viewrecords : true,
                colModel : itemInfoCM,
//				multiselect : true,
                pager : domObj.selNoListPg,
                gridComplete : function() {
                },
                onSelectRow : function(id) {
                    var rowData = domObj.$selNoListGrd.jqGrid('getRowData',id);
                    var statId = rowData.data_ext;
                   /* var stat = rowData.data_desc.split("-->");
                     var tool_stat = stat[1];*/
                    var tool_stat = rowData.data_item;
                    if(tool_stat=='4000'){
                        domObj.$pmEvtSeqId.attr(VAL.ENABLED_ATTR);
                        controlsFunc.iniToolPmSel();
                    }else {
                        domObj.$pmEvtSeqId.val("");
                        domObj.$pmEvtSeqId.attr(VAL.DISABLED_ATTR);
                    }
                    $("#selNoDialog").modal("hide");
                    function diaLogReasonFnc(){
                        var tool_id = domObj.$toolSel.val();
                        var rowId = domObj.$reasonListGrd.jqGrid('getGridParam','selrow');
                        var rowData  = domObj.$reasonListGrd.jqGrid('getRowData',rowId);
                        var tool_sub_stat = $("#statId").val();
                        var pm_evt_seq_id = $.trim(domObj.$pmEvtSeqId.val());


                        if(tool_stat=='4000'){
                            if(!pm_evt_seq_id){
                                showErrorDialog("", "保养计划不能为空!");
                                return false;
                            }
                        }
                       /* if(!rowId){
                            showErrorDialog("", "原因代码不能为空!");
                            return false;
                        }*/
                        var inObj,outObj;
                        var iary = {
                            tool_id : tool_id,
                            tool_stat  : tool_stat,
                            rsn_code : rowData.data_ext,
                            pm_evt_seq_id:pm_evt_seq_id
                        };
                        inObj ={
                            trx_id 		: VAL.T_FTPTOLOPE ,
                            action_flg : 'A',
                            evt_user	: VAL.EVT_USER,
                            iary:[iary]
                        };
                        outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            $("#reasonIdDialog").modal("hide");
                            showSuccessDialog("变更设备状态成功!");
                            var tool_id  = domObj.$toolSel.val();
                            controlsFunc.queryInfoBySel(tool_id);
                        }
                    }
                    $('#reasonIdDialog').modal({
                        backdrop:true,
                        keyboard:false,
                        show:false
                    });

                    $('#reasonIdDialog').unbind('shown.bs.modal');
                    $("#reasonIdDialog_sureBtn").unbind('click');

                    $('#reasonIdDialog').bind('shown.bs.modal');
                    $('#reasonIdDialog').modal("show");
                    $("#statId").val(statId);
                    controlsFunc.iniReasonListInfo();
                    domObj.$reasonListGrd.jqGrid("clearGridData");
                    controlsFunc.setTabGridInfo("TSRC","#reasonListGrd",statId,"");
                    $("#reasonIdDialog_sureBtn").bind('click',diaLogReasonFnc);
                }
            });
        },
        iniReasonListInfo:function(){
            var itemInfoCM = [
                {
                    name  : 'data_ext',
                    index : 'data_ext',
                    label : CODE_TAG,
                    width : 120
                },{
                    name : 'data_desc',
                    index :'data_desc',
                    label : CODE_DSC_TAG,
                    width : 120
                }
            ];
            domObj.$reasonListGrd.jqGrid({
                datatype : "local",
                autoheight : true,
                mtype : "POST",
                height : 270,
               /* autowidth : true,//宽度根据父元素自适应*/
                width:$(".container-fluid").width()*0.34,
                shrinkToFit : true,
                scroll : true,
                resizable : true,
                rownumbers : true,
                loadonce : true,
                viewrecords : true,
                colModel : itemInfoCM,
//				multiselect : true,
                pager : domObj.reasonListPg,
                gridComplete : function() {
                },
                onSelectRow : function(id) {
                }
            });
        },
        query_tool_his_func:function () {
            var tool_id = domObj.spans.$toolIdSpan.text();
            var inTrxObj = {
                trx_id : VAL.T_HIPTOLHIS,
                action_flg :'T',
                iary:[{
                    tool_id:tool_id
                }]
            }
            var outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(outTrxObj.rtn_code == VAL.NORMAL){
                setGridInfo(outTrxObj.oary,"#toolStatHisListGrd");
                // setGridInfo(outTrxObj.oaryB,"#portStatHisListGrd");
            }
        },

        queryInfoBySel:function(tool_id){
            var inObj, outObj;
            controlsFunc.iniSpanSelFnc();
            domObj.$portListGrd.jqGrid('clearGridData');
            var iary ={ tool_id:tool_id};

            inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'I',
                iary		:[iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                domObj.spans.$toolIdSpan   .html(outObj.oary[0].tool_id);
                domObj.spans.$toolDscSpan  .html(outObj.oary[0].tool_dsc);
                domObj.spans.$rotTolIdSpan .html(outObj.oary[0].root_tool_id);
                domObj.spans.$loadEndTimeSpan.html(outObj.oary[0].ld_last_fetch_timestamp);
                domObj.spans.$unloadEndTimeSpan.html(outObj.oary[0].ul_last_fetch_timestamp);
                if(outObj.oary[0].unit_typ === "UNIT"){
                    var inObjTrx = {
                        trx_id      : VAL.T_FBPBISTOL,
                        action_flg  : 'Q',
                        iary		:[{
                            tool_id : outObj.oary[0].root_tool_id
                        }]
                    };
                    var outObjTrx = comTrxSubSendPostJson(inObjTrx);
                    domObj.spans.$parentTolSpan.html(outObjTrx.oary[0].root_tool_id);
                }else {
                    domObj.spans.$parentTolSpan.html(outObj.oary[0].root_tool_id);
                }
                domObj.spans.$tolCateSpan.html(outObj.oary[0].tool_cate);
                domObj.spans.$tolModeSpan.html(outObj.oary[0].tool_run_mode);
                domObj.spans.$trnsCateSpan.html(outObj.oary[0].tool_trns_cate);
                domObj.spans.$tolStatSpan.html(outObj.oary[0].tool_stat);
                domObj.spans.$tolChdStatSpan.html(outObj.oary[0].tool_sub_stat);
                //获取设备状态信息
                var iary_EQPT = [{
                    data_cate : 'EQST',
                    data_id   : outObj.oary[0].tool_stat
                }]
                outTrxObj = controlsFunc.listDataInfo(iary_EQPT);
                if (outTrxObj.rtn_code) {
                    if(outTrxObj.oary.length>0){
                        domObj.spans.$statDscSpan.html(outTrxObj.oary[0].data_desc);
                    }
                }
            }
        },
        listDataInfo:function (iary) {
            var inObj = {
                trx_id :VAL.T_FBPBISDAT,
                action_flg: 'Q',
                iary : iary
            }
            var outTrx = comTrxSubSendPostJson(inObj);
            return outTrx;
        },
        iniOpeSel:function(){
            var inObj, outObj;

            inObj = {
                trx_id      : VAL.T_FBPBISOPE,
                action_flg  : 'L'
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                _setOpeSelectDate(outObj.tbl_cnt,outObj.oary, "ope_id", "ope_ver", "ope_dsc",domObj.$opeNoSel, true);
            }
        },
        iniToolPmSel :function(){
            var tool_id = $.trim(domObj.$toolSel.val());
            if(!tool_id){
                return;
            }
            var inObj = {
                trx_id      : VAL.T_MTPTOOLPM,
                action_flg  : 'M',
                tool_id_fk  : tool_id
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$pmEvtSeqId, outObj.oaryA, "evt_seq_id","pm_desc","",true);
            }
        },
        primaryToolGrid: function(ope_id,ope_ver){
            var inObj, outObj,inTrxObj,outTrxObj ,unit_typ;

            inTrxObj = {
                trx_id     : VAL.T_FBPBISOPE,
                action_flg : 'Q',
                iary 	   : [{
                    ope_id : ope_id,
                    ope_ver: ope_ver
                }]
            };
            outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if (outTrxObj.rtn_code == VAL.NORMAL) {
                var toolg_id = outTrxObj.oary[0].toolg_id;
                unit_typ = 'MAIN';
                var iary = {
                    tool_group_id_fk : toolg_id,
                    ope_id_fk : ope_id,
                    unit_typ : unit_typ
                };
                inObj = {
                    trx_id      : VAL.T_FBPBISTOL,
                    action_flg  : 'N',
                    iary        : [iary]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    setGridInfo(outObj.oary,"#toolSelListGrd");
                }
            }

        },
        childToolGrid : function(ope_id,ope_ver,unit_typ){
            var inObj, outObj,inTrxObj,outTrxObj ,unit_typ;
            var id = domObj.$toolSelListGrd.jqGrid('getGridParam','selrow');
            if (!id) {
//				$("input[type=radio]").attr("checked",'primary');
                showErrorDialog("","请先在表中选择设备！");
                domObj.$toolSelListGrd.jqGrid('clearGridData');
                return;
            }
            var rowData = domObj.$toolSelListGrd.jqGrid('getRowData',id);
            var root_tool_id = rowData.tool_id;
            inTrxObj = {
                trx_id     : VAL.T_FBPBISOPE,
                action_flg : 'Q',
                iary 	   : [{
                    ope_id : ope_id,
                    ope_ver: ope_ver
                }]
            };
            outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if (outTrxObj.rtn_code == VAL.NORMAL) {
                var tool_run_mode = outTrxObj.oary[0].tool_run_mode;
                var iary = {
                    tool_run_mode : tool_run_mode,
                    unit_typ:unit_typ,
                    root_tool_id:root_tool_id
                };
                inObj = {
                    trx_id      : VAL.T_FBPBISTOL,
                    action_flg  : 'H',
                    iary   : [iary]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    setGridInfo(outObj.oary,"#toolSelListGrd");
                }
            }
        },
        setTabGridInfo:function(data_cate,gridObj,data_id,data_ext){
            var iary = {
                data_cate : data_cate ,
                data_id   : data_id,
                data_ext  : data_ext
            };
            var inTrxObj = {
                trx_id      : 'FBPBISDAT' ,
                action_flg : 'Q'        ,
                iary        : [iary]
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                setGridInfo(outTrxObj.oary,gridObj);
            }
        },

        toolTrnFunc:function(){
            $('.modal-content').css({});
           // $('#selNoListDiv #gbox_selNoListGrd').css({"width": "283px","left": "50%","margin-left": "-141.5px"});
            $('.modal-content').css({"width":"600px","left":"50%","margin-left":"-300px"});
            $('#toolIdSelDiv').css({"margin-left":"-25px"});
            //模态框拖动
            $(".modal-content").draggable();
            var tool_trns_cate,tool_stat_id,tool_stat_ext,tool_stat;
            var ope_id 	 = domObj.$opeNoSel.val();
            var tool_id  = domObj.$toolSel.val();
            if (!ope_id) {
                showErrorDialog("","请选择站点！");
                return false;
            }
            if (!tool_id) {
                showErrorDialog("","请选择设备！");
                return false;
            }
            var inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'I',
                iary		:[{
                    tool_id: tool_id
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                tool_stat_id = outObj.oary[0].tool_stat;
            }
            if (tool_stat_id) {
                var iary = {
                    data_cate : 'EQST' ,
                    data_id   : tool_stat_id
                };
                var inTrxObj = {
                    trx_id      : 'FBPBISDAT',
                    action_flg : 'Q'        ,
                    iary        : [iary]
                };
                var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
                if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                    tool_stat_ext = outTrxObj.oary[0].data_ext;
                }
            }
            $('#selNoDialog').modal({
                backdrop:true,
                keyboard:false,
                show:false
            });

            $('#selNoDialog').unbind('shown.bs.modal');
            $("#selNoDialog_sureBtn").unbind('click');

            $('#selNoDialog').bind('shown.bs.modal');
            $('#selNoDialog').modal("show");
            controlsFunc.iniSelNoGridInfo();
            domObj.$selNoListGrd.jqGrid("clearGridData");
            if (tool_stat_ext) {
                var iary = {
                    data_cate : 'EQRL' ,
                   /* data_id   : tool_trns_cate,*/
                    data_ext  : tool_stat_ext
                };
                var inTrxObj = {
                    trx_id      : 'FBPBISDAT' ,
                    action_flg : 'Q'        ,
                    iary        : [iary]
                };
                var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
                if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                    var oary = outTrxObj.oary;
                    if (oary.length==0) {
                        showErrorDialog("","先设置变更规则！");
                        return false;
                    }
                    setGridInfo(oary,"#selNoListGrd");
                }
            }
            $("#selNoListGrd").setGridWidth($('.modal-content').width() - 100);
            $("#gbox_selNoListGrd").css({"left":"50%","margin-left": (-($('.modal-content').width() - 100)/2)});
        },

        toolPmFunc : function (){
            $('.modal-content').css({});
            // $('#selNoListDiv #gbox_selNoListGrd').css({"width": "283px","left": "50%","margin-left": "-141.5px"});
            $('.modal-content').css({"width":"600px","left":"50%","margin-left":"-300px"});
            $('#toolIdSelDiv').css({"margin-left":"-25px"});
            //模态框拖动
            $(".modal-content").draggable();
            var tool_trns_cate,tool_stat_id,tool_stat_ext,tool_stat;
            var ope_id 	 = domObj.$opeNoSel.val();
            var tool_id  = domObj.$toolSel.val();
            if (!ope_id) {
                showErrorDialog("","请选择站点！");
                return false;
            }
            if (!tool_id) {
                showErrorDialog("","请选择设备！");
                return false;
            }
            var inObj = {
                trx_id      : VAL.T_FBPBISTOL,
                action_flg  : 'I',
                iary		:[{
                    tool_id: tool_id
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                tool_stat_id = outObj.oary[0].tool_stat;
            }
            if (tool_stat_id) {
                var iary = {
                    data_cate : 'EQST' ,
                    data_id   : tool_stat_id
                };
                var inTrxObj = {
                    trx_id      : 'FBPBISDAT',
                    action_flg : 'Q'        ,
                    iary        : [iary]
                };
                var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
                if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                    tool_stat_ext = outTrxObj.oary[0].data_ext;
                }
            }

            //切换PM状态
            tool_stat ='4000';
            if(tool_stat=='4000'){
                domObj.$pmEvtSeqId.attr(VAL.ENABLED_ATTR);
                controlsFunc.iniToolPmSel();
            }
            $("#selNoDialog").modal("hide");
            function diaLogReasonFnc2(){
                var tool_id = domObj.$toolSel.val();
                // var rowId = domObj.$reasonListGrd.jqGrid('getGridParam','selrow');
                // var rowData  = domObj.$reasonListGrd.jqGrid('getRowData',rowId);
                // var tool_sub_stat = $("#statId").val();
                var pm_evt_seq_id = $.trim(domObj.$pmEvtSeqId.val());
                if(tool_stat=='4000'){
                    if(!pm_evt_seq_id){
                        showErrorDialog("", "保养计划不能为空!");
                        return false;
                    }
                }
                /* if(!rowId){
                 showErrorDialog("", "原因代码不能为空!");
                 return false;
                 }*/
                var inObj,outObj;
                var iary = {
                    tool_id : tool_id,
                    tool_stat  : tool_stat,
                    // rsn_code : rowData.data_ext,
                    pm_evt_seq_id:pm_evt_seq_id
                };
                inObj ={
                    trx_id 		: VAL.T_FTPTOLOPE ,
                    action_flg : 'A',
                    evt_user	: VAL.EVT_USER,
                    iary:[iary]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    $("#reasonIdDialog").modal("hide");
                    showSuccessDialog("变更设备状态成功!");
                    var tool_id  = domObj.$toolSel.val();
                    controlsFunc.queryInfoBySel(tool_id);
                }
            }

            $('#reasonIdDialog').modal({
                backdrop:true,
                keyboard:false,
                show:false
            });

            $('#reasonIdDialog').unbind('shown.bs.modal');
            $("#reasonIdDialog_sureBtn").unbind('click');

            $('#reasonIdDialog').bind('shown.bs.modal');
            $('#reasonIdDialog').modal("show");
            $("#statId").val($("#tolChdStatSpan").text());
            controlsFunc.iniReasonListInfo();
            domObj.$reasonListGrd.jqGrid("clearGridData");
            controlsFunc.setTabGridInfo("TSRC","#reasonListGrd",statId,"");
            $("#reasonIdDialog_sureBtn").bind('click',diaLogReasonFnc2);

        },

        toolSelFunc: function(){
            $('.modal-content').css({"width":"600px","left":"50%","margin-left":"-300px"});
            $('#toolIdSelDiv').css({"margin-left":"-25px"});
            //模态框拖动
            $(".modal-content").draggable();
            domObj.$toolSel.val("");
            var opeSelVal = domObj.$opeNoSel.find("option:selected").val();
            if (!opeSelVal) {
                showErrorDialog("","请选择站点！");
                return false;
            }
            var ope_id = opeSelVal.split("@","2")[0];
            var ope_ver = opeSelVal.split("@","2")[1];
            function diaLogSelToolFnc(){
                var id = domObj.$toolSelListGrd.jqGrid('getGridParam','selrow');
                var rowData = domObj.$toolSelListGrd.jqGrid('getRowData',id);
                domObj.$toolSel.val(rowData.tool_id);
                var tool_id  = rowData.tool_id
                if (!tool_id) {
                    showErrorDialog("","请选择设备！");
                    return false;
                }
                controlsFunc.queryInfoBySel(tool_id);
                $("#toolIdSelDialog").modal("hide");
            };
            $('#toolIdSelDialog').modal({
                backdrop:true,
                keyboard:false,
                show:false
            });
            $('#toolIdSelDialog').unbind('shown.bs.modal');
            $("#toolIdSelDialog_sureBtn").unbind('click');

            $('#toolIdSelDialog').bind('shown.bs.modal');
            $('#toolIdSelDialog').modal("show");
            controlsFunc.iniToolGridInfo();
            domObj.$toolSelListGrd.jqGrid('clearGridData');
            $("#primaryRadio,#childRadio,#unitRadio").removeAttr("checked");
            $("#primaryRadio").unbind("click");
            $("#childRadio").unbind("click");
            $("#unitRadio").unbind("click");
            $("#primaryRadio").click(function(){
                controlsFunc.primaryToolGrid(ope_id,ope_ver);
                controlsFunc.savePreRaioData(true);
            });
            $("#childRadio").click(function(){
                controlsFunc.childToolGrid(ope_id,ope_ver,"SUB");
                controlsFunc.savePreRaioData(false);
            });
            $("#unitRadio").click(function(){
                controlsFunc.childToolGrid(ope_id,ope_ver,"UNIT");
            });
            $("#toolIdSelDialog_sureBtn").bind('click',diaLogSelToolFnc);
            $("#toolSelListGrd").setGridWidth($('.modal-content').width() - 100);
            $("#gbox_toolSelListGrd").css({"left":"50%","margin-left": (-($('.modal-content').width() - 100)/2) + 30});
        },
        toolLockFunc: function (){
            var tool_id = domObj.$toolSel.val();
            if (!tool_id) {
                showErrorDialog("","请选择机台！");
                return;
            }
            var inObj,outObj;
            var iary = {
                tool_id : tool_id,
                tool_lock_flg: 'Y'
            };
            inObj ={
                trx_id 		: VAL.T_FTPTOLOPE ,
                action_flg : 'C',
                evt_usr     : VAL.EVT_USER,
                iary:[iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                showSuccessDialog("设备锁定成功!");
            }
        },
        toolUnlockFunc: function (){
            var tool_id = domObj.$toolSel.val();
            if (!tool_id) {
                showErrorDialog("","请选择机台！");
                return;
            }
            var inObj,outObj;
            var iary = {
                tool_id : tool_id,
                tool_lock_flg: 'N'
            };
            inObj ={
                trx_id 		: VAL.T_FTPTOLOPE ,
                action_flg : 'D',
                evt_usr     : VAL.EVT_USER,
                iary:[iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                showSuccessDialog("设备解锁成功!");
            }
        },
        toolHoldFunc:function (){
            var tool_id = domObj.$toolSel.val();
            if (!tool_id) {
                showErrorDialog("","请选择机台！");
                return;
            }
            var inObj,outObj;
            var iary = {
                tool_id : tool_id,
                tool_hold_flg: 'Y'
            };
            inObj ={
                trx_id 		: VAL.T_FTPTOLOPE ,
                action_flg : 'E',
                evt_usr     : VAL.EVT_USER,
                iary:[iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                showSuccessDialog("设备HOLD成功!");
            }
        },
        toolUnholdFunc:function (){
            var tool_id = domObj.$toolSel.val();
            if (!tool_id) {
                showErrorDialog("","请选择机台！");
                return;
            }
            var inObj,outObj;
            var iary = {
                tool_id : tool_id,
                tool_hold_flg: 'N'
            };
            inObj ={
                trx_id 		: VAL.T_FTPTOLOPE ,
                action_flg : 'F',
                evt_usr     : VAL.EVT_USER,
                iary:[iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                showSuccessDialog("设备解HOLD成功!");
            }
        },
        toolRemarkFunc: function(){
            var tool_id = domObj.$toolSel.val();
            if (!tool_id) {
                showErrorDialog("","请选择机台！");
                return;
            }
            var remarkInfo = domObj.$remarkInfo.val();
            if(!remarkInfo){
                showErrorDialog("","备注信息不能为空！");
                return;
            }
            var inObj,outObj;
            var iary = {
                tool_id : tool_id,
                tool_note: remarkInfo
            };
            inObj ={
                trx_id 		: VAL.T_FTPTOLOPE ,
                action_flg : 'G',
                evt_usr     : VAL.EVT_USER,
                iary:[iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                showSuccessDialog("设备备注成功!");
            }
        },
        trnModeFunc: function(){
            var tool_id = domObj.$toolSel.val();
            if (!tool_id) {
                showErrorDialog("","请选择机台！");
                return;
            }
            $('#toolModeDialog').modal({
                backdrop:true,
                keyboard:false,
                show:false
            });
            function diaLogSelModeFnc(){
                var tool_run_mode = $("#modeId").val();
                var inObj,outObj;
                var iary = {
                    tool_id : tool_id,
                    tool_run_mode: tool_run_mode
                };
                inObj ={
                    trx_id 		: VAL.T_FTPTOLOPE ,
                    action_flg : 'H',
                    evt_usr     : VAL.EVT_USER,
                    iary:[iary]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    $('#toolModeDialog').modal("hide");
                    showSuccessDialog("设备模式切换成功!");
                }
            }
            $('#toolModeDialog').unbind('shown.bs.modal');
            $("#toolModeDialog_sureBtn").unbind('click');

            $('#toolModeDialog').bind('shown.bs.modal');
            $('#toolModeDialog').modal("show");
            $("#modeId").empty();
            comAddValueByDataCateFnc("#modeId",'TRMD','data_ext','data_ext', false);
            $('#toolModeDialog_sureBtn').bind('click',diaLogSelModeFnc);
        },
        savePreRaioData : function(flag){
            $("#unitRadio").attr("disabled",flag);
        },
        iniSpanSelFnc : function(){
            domObj.spans.$toolIdSpan       .html("");
            domObj.spans.$toolDscSpan      .html("");
            domObj.spans.$rotTolIdSpan     .html("");
            domObj.spans.$parentTolSpan    .html("");
            domObj.spans.$tolCateSpan      .html("");
            domObj.spans.$tolModeSpan      .html("");
            domObj.spans.$trnsCateSpan     .html("");
            domObj.spans.$tolStatSpan      .html("");
            domObj.spans.$tolChdStatSpan   .html("");
            domObj.spans.$ctrlDscSpan      .html("");
            domObj.spans.$recipeIdSpan     .html("");
            domObj.spans.$frtTolChdSpan    .html("");
            domObj.spans.$rcUpdUsrSpan     .html("");
            domObj.spans.$rcUpdTimSpan     .html("");
            domObj.spans.$statDscSpan	   .html("");
            domObj.spans.$loadEndTimeSpan  .html("");
            domObj.spans.$unloadEndTimeSpan.html("");
        }
    };
    var iniButtonAction =function(){
        domObj.buttons.$tool_trns_btn.click(function(){
            controlsFunc.toolTrnFunc();
        });

        domObj.buttons.$tool_pm_btn.click(function(){
            controlsFunc.toolPmFunc();
        });

        domObj.buttons.$port_trns_btn.click(function(){
            controlsFunc.protTrnFunc();
        });
        domObj.buttons.$tool_lock_btn.click(function(){
            controlsFunc.toolLockFunc();
        });
        domObj.buttons.$tool_unlock_btn.click(function(){
            controlsFunc.toolUnlockFunc();
        });
        domObj.buttons.$tool_hold_btn.click(function(){
            controlsFunc.toolHoldFunc();
        });
        domObj.buttons.$tool_unhold_btn.click(function(){
            controlsFunc.toolUnholdFunc();
        });
        domObj.buttons.$query_tool_his_btn.click(function () {
            controlsFunc.query_tool_his_func();
        })
        domObj.buttons.$tool_remark_btn.click(function(){
            controlsFunc.toolRemarkFunc();
        });
        domObj.buttons.$tool_trnMode_btn.click(function(){
            controlsFunc.trnModeFunc();
        });
        domObj.$toolSel.focus(function(){
            controlsFunc.toolSelFunc();
        });
        domObj.$opeNoSel.change(function(){
            domObj.$toolSel.val("");
        });
    };

    function initFunc(){
        controlsFunc.iniToolStatHisInfo();
        controlsFunc.iniOpeSel();
        controlsFunc.iniSpanSelFnc();
        domObj.$remarkInfo.val("");
        domObj.$toolSel.val("");
        iniButtonAction();
    }
    initFunc();
   /* $('.modal-content').css({"width":"550px","left":"50%","margin-left":"-165px"});
    $('#toolIdSelDiv').css({"margin-left":"-25px"});*/
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#divTotal").width();
        offsetBottom =  $(window).height() - $("#divTotal").offset().top;
        $("#divTotal").height(offsetBottom * 0.99);
        $("#toolStatHisListGrd").setGridWidth(divWidth * 0.99);
        $("#toolStatHisListGrd").setGridHeight(offsetBottom * 0.80 - 121);

    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });

});